﻿using AmuTools;
using app.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace app.Api
{
    public class ApiPlateformService : ApiBase
    {
        public ApiPlateformService(Controller ctx) : base(ctx) { }

        #region 自定义请求参数
        // 作者

        #endregion

        public void Do(string action)
        {
            switch (action)
            {
                case "get":
                    {
                        PlateformService a = G.DB.GetById<PlateformService>(req_id);
                        ResponseJson(new ObjectResult { data = a });
                        break;
                    }
                case "isvisiable":
                    {
                        PlateformService a = G.DB.GetById<PlateformService>(req_id);
                        a.isvisiable = GetIntParam(new ReqIntParamOption { Name = "isvisiable" });
                        G.DB.Update(a);
                        ResponseJson(new Result { });
                        break;
                    }
                case "get_all_service":
                    {
                        String title = GetStringParam(new ReqStringParamOption { Name = "title", Level = REQPARAMLEVEL.FREE });
                        String number = GetStringParam(new ReqStringParamOption { Name = "number", Level = REQPARAMLEVEL.FREE });
                        Cd cd = new Cd();
                        if (title != "") cd.And("title like '%{0}%'", title);
                        if (number != "") cd.And("number like '%{0}%'", number);
                        Cd visiable_cd = new Cd("isvisiable = 1");
                        if (UserRole.Equals((int)ROLE_TYPES.ADMIN))
                        {
                            visiable_cd.Or("isvisiable = 0");
                        }
                        Cd status_cd = new Cd("status = 2");
                        if (UserRole.Equals((int)ROLE_TYPES.ADMIN))
                        {
                            status_cd.Or("status = 1");
                        }
                        cd.And(visiable_cd).And(status_cd);
                        Ob ob = new Ob();
                        ob.Desc("create_time");
                        ResponseJson(GetPageResult<PlateformService>(cd.Render(), ob.Render()));
                        break;
                    }
                case "post":
                    {
                        string now = DateTimeEx.GetNowTimeStamp();
                        PlateformService a = new PlateformService {
                            content = req_content,
                            title = req_title,
                            appendix_ids = req_appendix_ids,
                            create_time = now,
                            status = 1,};
                        G.DB.Insert<PlateformService>(a);
                        a.number = "ps" + a.id.ToString().PadLeft(6,'0');
                        G.DB.Update<PlateformService>(a);
                        ResponseJson(new IDResult { id = a.id });
                        break;
                    }
                case "modify":
                    {
                        PlateformService a = G.DB.GetById<PlateformService>(req_id);
                        a.content = req_content;
                        a.title = req_title;
                        a.appendix_ids = GetStringParam(new ReqStringParamOption { Name = "appendix_ids", Level = REQPARAMLEVEL.FREE });
                        a.process = GetStringParam(new ReqStringParamOption { Name = "process", Level = REQPARAMLEVEL.FREE });
                        a.status = 2;
                        G.DB.Update<PlateformService>(a);
                        ResponseJson(new Result());
                        break;
                    }
                case "delete":
                    {
                        PlateformService a = G.DB.GetById<PlateformService>(req_id);
                        a.status = 0;
                        if (G.DB.Update<PlateformService>(a)>0)
                        {
                            ResponseJson(new BooleanResult { value = true });
                        }
                        else
                        {
                            ResponseJson(new BooleanResult { value = false });
                        }
                        
                        break;
                    }
                default: break;
            }
        }
        public void Abutment(string action)
        {
            switch (action)
            {
                case "get":
                    {
                        PlateformServiceAbutment service = G.DB.GetById<PlateformServiceAbutment>(req_id);
                        ResponseJson(new ObjectResult { data = service });
                        break;
                    }
                case "pass":
                    {
                        PlateformServiceAbutment p = G.DB.GetById<PlateformServiceAbutment>(req_id);
                        p.status = 2;

                        if (G.DB.Update<PlateformServiceAbutment>(p) > 0)
                        {
                            ResponseJson(new BooleanResult { value = true });
                        }
                        else
                        {
                            ResponseJson(new BooleanResult { value = false });
                        }
                        break;
                    }
                case "post":
                    {
                        Cd cd = new Cd();
                        cd.And("company_id = {0}", UserCode);
                        int plateformservice_id = GetIntParam(new ReqIntParamOption { Name = "plateformservice_id" });
                        cd.And("plateformservice_id = {0}", plateformservice_id);
                        Ob ob = new Ob();
                        
                        if (!(G.DB.GetCount<PlateformServiceAbutment>(cd.Render())>0))
                        {
                            PlateformServiceAbutment psa = new PlateformServiceAbutment();
                            int company_id = Int32.Parse(UserCode);
                            psa.company_id = company_id;
                            psa.plateformservice_id = plateformservice_id;
                            PlateformService p = G.DB.GetById<PlateformService>(psa.plateformservice_id);
                            psa.title = p.title;
                            psa.create_time = DateTimeEx.DateTimeToTimeStamp(DateTime.Now);
                            psa.number = "a" + p.number + company_id;
                            psa.service_number = p.number;
                            psa.status = 1;
                            if (G.DB.Insert<PlateformServiceAbutment>(psa) > 0)
                            {
                                ResponseJson(new IDResult { id = psa.id });
                            }
                            else
                            {
                                ThrowStatus(STATUS.FAILED_INSERT, "操作失败");
                            }
                        }
                        else
                        {
                            ThrowStatus(STATUS.FAILED_INSERT, "您已申请过此服务");
                        }
                        
                        break;
                    }
                case "getall":
                    {
                        int status = GetIntParam(new ReqIntParamOption { Name = "status", Level = REQPARAMLEVEL.FREE ,DefaultValue=-1});
                        string title = GetStringParam(new ReqStringParamOption { Name = "title", Level = REQPARAMLEVEL.FREE });
                        string number = GetStringParam(new ReqStringParamOption { Name = "number", Level = REQPARAMLEVEL.FREE });
                        string service_number = GetStringParam(new ReqStringParamOption { Name = "service_number", Level = REQPARAMLEVEL.FREE });
                        string check_status = GetStringParam(new ReqStringParamOption { Name = "check_status",Level = REQPARAMLEVEL.FREE ,DefaultValue="-1"});
                        Cd cd = new Cd();
                        if (title != "") cd.And("title like '%{0}%'", title);
                        if (number != "") cd.And("number like '%{0}%'", number);
                        if (service_number != "") cd.And("service_number like '%{0}%'",service_number);
                        if (status !=-1) cd.And("status = {0}",status);
                        if (check_status != "-1") cd.And("status ='{0}'",check_status);
                        if (req_begin_time != "") cd.And("create_time>='{0}'", req_begin_time);
                        if (req_end_time != "") cd.And("create_time<='{0}'", req_end_time);
                        Ob ob = new Ob();
                        ob.Desc("create_time");
                        if (req_excel)
                        {
                            DataTable dt = GetSqlResult<PlateformServiceAbutment>(cd.Render(), ob.Render()).FirstTable;
                            ResponseExcel(dt, PlateformServiceAbutment.ExcelColumns, PlateformServiceAbutment.ExcelColumnsHandle);
                        }
                        else
                        {
                            ResponseJson(GetPageResult<PlateformServiceAbutment>(cd.Render(), ob.Render()));
                        }
                        break;
                    }
                case "delete":
                    {
                        int value = G.DB.Delete<PlateformServiceAbutment>(req_id);
                        if (value != 0)
                        {
                            ResponseJson(new BooleanResult { value = true, message = "撤销申领成功" });
                        }
                        else
                        {
                            ResponseJson(new BooleanResult { value = false, message = "撤销申领失败" });
                        }
                        break;
                    }
            }
        }
    }
}